package cn.ito.zjgsu.domain.wrapper;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.ito.zjgsu.domain.Authority;
import cn.ito.zjgsu.domain.Role;
import cn.ito.zjgsu.service.RolePermissionService;
import cn.ito.zjgsu.utils.util.CollectionUtil;
@Service
public class RoleWrapper {
	
	private static RolePermissionService rolePermissionService;
	
	
	public RolePermissionService getRolePermissionService() {
		return rolePermissionService;
	}
	@Resource
	public void setRolePermissionService(RolePermissionService rolePermissionService) {
		RoleWrapper.rolePermissionService = rolePermissionService;
	}
	

	private Role role;
	private List<AuthorityWrapper> permissions;
	
	
	
	public RoleWrapper(){}
	public RoleWrapper(Role role){
		this.role = role;
		System.out.println(rolePermissionService == null);
		List<Authority> as = rolePermissionService.getPermissions(role.getRoleId());
		permissions = AuthorityWrapper.WrapAuthority(rolePermissionService.getPermissions(role.getRoleId())) != null ?
				AuthorityWrapper.WrapAuthority(rolePermissionService.getPermissions(role.getRoleId())):
					new ArrayList<AuthorityWrapper>();
	}
	public RoleWrapper(Role role , List<AuthorityWrapper> permissions){
		this.role = role;
		this.permissions = permissions;
	}
	
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	public List<AuthorityWrapper> getPermissions() {
		return permissions;
	}
	public void setPermissions(List<AuthorityWrapper> permissions) {
		this.permissions = permissions;
	}
	
	
	public List<String> getStringPermissions(){
		List<String> stringPermissions = new ArrayList<String>();
		for(AuthorityWrapper authority : permissions){
			stringPermissions.add(authority.getAuthority().getAuthorityName());
		}
		return stringPermissions;
	}
	
	public String getRoleName(){
		return role.getRoleName();
	}
	
	
	
	public String toJsonObjectString(){	
		return toJsonObject().toJSONString();
	}
	
	public JSONObject toJsonObject(){
		JSONObject json = new JSONObject();	
		initUserField(json);		
		return json;
	}

	private void initUserField(JSONObject json) {
		if(role!= null){
			json.put("roleCreatetime", role.getRoleCreatetime());
			json.put("roleDescription", role.getRoleDescription());
			json.put("roleName", role.getRoleName());
			json.put("id", role.getRoleId());
		}
		if(!CollectionUtil.isSafeEmpty(permissions)){
			json.put("permissions", AuthorityWrapper.toJsonArray(permissions));	
		}
		
	}
	
	
	public static JSONArray toJsonArray(List<RoleWrapper> roleWrapper){
		JSONArray array = new JSONArray();
		for(RoleWrapper each : roleWrapper){
			array.add(each.toJsonObject());
		}
		return array;
	}
	
	public static String toJsonArrayString(JSONArray array){
		return array.toJSONString();
	}
	public static String toJsonArrayString(List<RoleWrapper> roleWrapper){
		return toJsonArray(roleWrapper).toJSONString();
	}
	
	public static List<RoleWrapper> wrapRoleList(List<Role> roles){
		List<RoleWrapper> rws = new ArrayList<RoleWrapper>();
		for(Role r : roles){
			rws.add(new RoleWrapper(r));
		}
		return rws;
	}
	
	public static JSONArray list2JsonArray(List<Role> lrws){
		if(lrws != null){
			return toJsonArray(RoleWrapper.wrapRoleList(lrws));
		}else{
			return null;
		}
		
	}

	
}
